package widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;

import net.ck.hzq.R;

import Utils.ScreenUtil;

/**
 * @author BF00233
 */

public class BottomIndicator extends View implements IPagerIndicator {

    private int mSelectedPosition;

    private float mGap;
    private int mSelectedColor;
    private int mUnselectedColor;
    private int mCount;
    private Paint mPaint;
    private float mRadius;
    private Bitmap mSelectedBitmap;
    private Bitmap mUnSelectedBitmap;
    private int mBitmapWidth;
    private int mBitmapHeight;
    private float mStartOffset;

    public BottomIndicator(Context context) {
        this(context, null);
    }

    public BottomIndicator(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BottomIndicator(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }

    private void init(AttributeSet attrs) {
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.BottomIndicator);
        mGap = typedArray.getDimension(R.styleable.BottomIndicator_ui_gap, ScreenUtil.dip2px(getContext(), 6.5f));
        mRadius = typedArray.getDimension(R.styleable.BottomIndicator_ui_gap, ScreenUtil.dip2px(getContext(), 3));
        mSelectedColor = typedArray.getColor(R.styleable.BottomIndicator_ui_selected_color, getResources().getColor(R.color.white));
        mUnselectedColor = typedArray.getColor(R.styleable.BottomIndicator_ui_unselected_color, Color.GRAY);

        int selectedResourceId = typedArray.getResourceId(R.styleable.BottomIndicator_ui_selected_bitmap, -1);
        int unSelectedResourceId = typedArray.getResourceId(R.styleable.BottomIndicator_ui_unselected_bitmap, -1);
        // TODO 同一个图片资源放在不同的资源文件夹下，会有不同的大小呈现
        if (selectedResourceId != -1 && unSelectedResourceId != -1) {
            mSelectedBitmap = ((BitmapDrawable) getResources().getDrawable(selectedResourceId)).getBitmap();
            mUnSelectedBitmap = ((BitmapDrawable) getResources().getDrawable(unSelectedResourceId)).getBitmap();
            mBitmapWidth = Math.max(mSelectedBitmap.getWidth(), mUnSelectedBitmap.getWidth());
            mBitmapHeight = Math.max(mSelectedBitmap.getHeight(), mUnSelectedBitmap.getHeight());
        }

        typedArray.recycle();
        mPaint = new Paint();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (useBitmap()) {
            setMeasuredDimension(widthMeasureSpec, mBitmapHeight + 1);
        } else {
            setMeasuredDimension(widthMeasureSpec, (int) (mRadius * 2 + 1));
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();
        canvas.translate(mStartOffset, 0);
        for (int i = 0; i < mCount; i++) {
            if (i == mSelectedPosition) {
                if (mSelectedBitmap != null) {
                    canvas.drawBitmap(mSelectedBitmap, (mBitmapWidth + mGap) * i, 0, mPaint);
                } else {
                    mPaint.setColor(mSelectedColor);
                    canvas.drawCircle(mRadius * (2 * i + 1) + mGap * i, mRadius, mRadius, mPaint);
                }
            } else {
                if (mUnSelectedBitmap != null) {
                    canvas.drawBitmap(mUnSelectedBitmap, (mBitmapWidth + mGap) * i, 0, mPaint);
                } else {
                    mPaint.setColor(mUnselectedColor);
                    canvas.drawCircle(mRadius * (2 * i + 1) + mGap * i, mRadius, mRadius, mPaint);
                }
            }
        }
        canvas.restore();
    }

    /**
     * 不可无限滑动
     */
    public void setUpWithViewPager(final ViewPager viewPager, boolean infinite) {
        mSelectedPosition = 0;
        if (!infinite) {
            mCount = viewPager.getAdapter().getCount();
            viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                }

                @Override
                public void onPageSelected(int position) {
                    BottomIndicator.this.onPageSelected(position);
                }

                @Override
                public void onPageScrollStateChanged(int state) {
                }
            });
        } else {
            mCount = viewPager.getAdapter().getCount() - 2;
            viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                }

                @Override
                public void onPageSelected(int position) {
                    if (position == 0 || position == mCount + 1) {
                        return;
                    }

                    int newPosition = position - 1;
                    BottomIndicator.this.onPageSelected(newPosition);
                }

                @Override
                public void onPageScrollStateChanged(int state) {
                }
            });
        }

        setCanvasStartOffset();
        invalidate();
    }

    private void setCanvasStartOffset() {
        if (useBitmap()) {
            mStartOffset = ((getResources().getDisplayMetrics().widthPixels - mBitmapWidth * mCount - mGap * (mCount - 1) + 1)) / 2;
        } else {
            mStartOffset = ((getResources().getDisplayMetrics().widthPixels - mRadius * 2 * mCount - mGap * (mCount - 1) + 1)) / 2;
        }
    }

    private boolean useBitmap() {
        return mBitmapWidth > 0 && mBitmapHeight > 0;
    }

    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        mSelectedPosition = i;
        invalidate();
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }

    public Bitmap getmSelectedBitmap() {
        return mSelectedBitmap;
    }

    public void setIndicatorBitmap(int selectDrawable, int unSelectDrawable) {
        this.mSelectedBitmap = ((BitmapDrawable) ContextCompat.getDrawable(getContext(), selectDrawable)).getBitmap();
        this.mUnSelectedBitmap = ((BitmapDrawable) ContextCompat.getDrawable(getContext(), unSelectDrawable)).getBitmap();
        mBitmapWidth = Math.max(mSelectedBitmap.getWidth(), mUnSelectedBitmap.getWidth());
        mBitmapHeight = Math.max(mSelectedBitmap.getHeight(), mUnSelectedBitmap.getHeight());
    }

    public Bitmap getmUnSelectedBitmap() {
        return mUnSelectedBitmap;
    }
}
